/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/24 15:11
 */
package org.mechanic.fund.service.product.impl;

import org.mechanic.core.Constants;
import org.mechanic.fund.domain.product.Product;
import org.mechanic.fund.domain.product.ProductAttachment;
import org.mechanic.fund.domain.product.ProductAttachmentType;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.product.ProductAttachmentRepository;
import org.mechanic.fund.service.product.IProductAttachmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 产品附件服务接口
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class ProductAttachmentService implements IProductAttachmentService {

    private static Logger logger = LoggerFactory.getLogger(ProductTypeService.class);

    private ProductAttachmentRepository attachmentRepository;

    @Autowired
    public ProductAttachmentService(ProductAttachmentRepository attachmentRepository) {this.attachmentRepository = attachmentRepository;}

    /**
     * 返回匹配指定产品编号和附件类型编号的产品附件列表
     * 排序方式：按创建时间降序
     *
     * @param productId 产品编号
     * @param typeId    附件类型编号
     * @return 产品附件列表
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<ProductAttachment> getProductAttachmentList(String productId, String typeId, PageRequest pageRequest) {
        Assert.hasLength(productId, "productId 不能为空");
        Assert.hasLength(typeId, "typeId 不能为空");

        Product product = new Product();
        product.setId(productId);

        ProductAttachmentType type = new ProductAttachmentType();
        type.setId(typeId);

        return this.attachmentRepository.findByProductAndProductAttachmentTypeOrderByCreateTimeDesc(product, type, pageRequest);
    }

    /**
     * 返回匹配指定主键编号的对象
     *
     * @param id 主键编号
     * @return 对象实体
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public ProductAttachment getById(String id) {
        return this.attachmentRepository.findOne(id);
    }

    /**
     * 保存。 主键为空则create，不为空则update。
     *
     * @param productAttachments 被保存的对象集合
     */
    @Override
    public void save(List<ProductAttachment> productAttachments, String operationUserId) {
        this.attachmentRepository.save(DtoBeanMapper.setOperationToList(productAttachments, operationUserId));
    }

    /**
     * 删除匹配主键ID的对象
     *
     * @param id 主键ID
     */
    @Override
    public void delete(String id) {
        try {
            this.attachmentRepository.delete(id);
        } catch (EmptyResultDataAccessException e) {
            logger.info(id + "不存在");
        }
    }
}
